package sapling.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sapling.api.model.ApiSearch;
import sapling.api.model.ApiSearchModel;
import sapling.api.model.ApiTreeWell;
import sapling.dao.AreaLocationDao;
import sapling.dao.LandMassDao;
import sapling.dao.TreeDao;
import sapling.dao.TreeManyDao;
import sapling.form.LandMassForm;
import sapling.form.SeekTreeForm;
import sapling.model.AreaLocation;
import sapling.model.LandMass;
import sapling.model.Tree;
import sapling.model.TreeWell;
import sapling.service.AreaLocationService;
import sapling.service.LandMassService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by ever on 2016/10/9.
 */
@Controller
@RequestMapping(value = "/admin/landMass")
public class LandMassAction {

    @Autowired
    private LandMassDao landMassDao;
    @Autowired
    private AreaLocationDao areaLocationDao;
    @Autowired
    private AreaLocationService areaLocationService;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private LandMassService landMassService;
    @Autowired
    private TreeManyDao treeManyDao;


    @RequestMapping("/manage")
    public String manage(Model model) {
        model.addAttribute("area",areaLocationDao.findAll(new Sort(Sort.Direction.ASC,"serialNumber")));
        return "admin/landMass/manage";
    }

    @RequestMapping("/sign")
    public String sign(Model model) {
        model.addAttribute("area",areaLocationDao.findAll(new Sort(Sort.Direction.ASC,"serialNumber")));
        return "admin/landMass/sign";
    }

    @RequestMapping("/put")
    public String put(Model model) {
        model.addAttribute("area",areaLocationDao.findAll(new Sort(Sort.Direction.ASC,"serialNumber")));
        return "admin/landMass/put";
    }

    @RequestMapping("/out")
    public String out(Model model) {
        model.addAttribute("area",areaLocationDao.findAll(new Sort(Sort.Direction.ASC,"serialNumber")));
        return "admin/landMass/out";
    }

    @RequestMapping("/die")
    public String die(Model model) {
        model.addAttribute("area",areaLocationDao.findAll(new Sort(Sort.Direction.ASC,"serialNumber")));
        return "admin/landMass/die";
    }

    @RequestMapping("/landMass")
    public String treeType(String serialNumber,Long areaid,int type,Model model) {
        String strUrl = "";
        if(type==0){
            strUrl="admin/landMass/manage";
        }else if(type==1){
            strUrl = "admin/landMass/put";
        }else if(type==2){
            strUrl = "admin/landMass/out";
        }else if(type==3){
            strUrl = "admin/landMass/sign";
        }else if(type==4){
            strUrl = "admin/landMass/die";
        }
        model.addAttribute("area",areaLocationDao.findAll(new Sort(Sort.Direction.ASC,"serialNumber")));
        AreaLocation areaLocations = areaLocationDao.findOne(areaid);
        if(Objects.nonNull(areaLocations)){
            LandMass landMass = landMassDao.findSerialNumber(areaid, serialNumber);
            if(Objects.nonNull(landMass)){
                strUrl = "admin/treeWell/treeWell_create";
                List<ApiTreeWell> apiTreeWells = landMassService.treeWell(landMass.getLandMass_id(), null);
                if (Objects.nonNull(landMass.getLandMass_id())) {
                    List<Tree> trees = treeDao.findTreeSpeciesGroup(landMass.getLandMass_id());
                    int index = 1;
                    for (Tree tree : trees) {
                        model.addAttribute("tree" + index++, this.statistics(tree.getTreeSpecies(), landMass.getLandMass_id()));
                    }
                }
                model.addAttribute("collect",landMassService.collect(landMass.getLandMass_id()));
                model.addAttribute("landMass", landMass);
                model.addAttribute("treeWell", apiTreeWells);
                if (Objects.nonNull(apiTreeWells) && apiTreeWells.size() > 0) {
                    if(type==0){
                        strUrl ="admin/treeWell/treeWell_list";
                    }else if(type==1){
                        strUrl = "admin/treeWell/put";
                    }else if(type==2){
                        strUrl = "admin/treeWell/out";
                    }else if(type==3){
                        strUrl = "admin/treeWell/sign";
                    }else if(type==4){
                        strUrl = "admin/treeWell/die";
                    }
                }
                return strUrl;
            }
        }
        return strUrl;
    }

    private String statistics(String statistics, Long landMass_id) {
        String str = statistics;
        List<Tree> trees1 = treeDao.trees035(statistics, landMass_id);
        if (Objects.nonNull(trees1) && trees1.size() > 0) {
            str += " " + trees1.size() + "棵";
        }
        return str;
    }

    @RequestMapping("/treeSpecies")
    public String treeSpecies(SeekTreeForm form, Model model) {
        String str="admin/landMass/landMass_list";
        if(form.getBranch_max()!=null||form.getBranch_min()!=null){
            form.setClump((form.getBranch_min()>0||form.getBranch_max()>0)?1:0);
        }else {
            form.setClump(0);
        }
        if (Objects.nonNull(form.getLandMass_id())&&form.getReq()==1) {
            model.addAttribute("landMass", landMassDao.findOne(form.getLandMass_id()));
            model.addAttribute("treeWell", landMassService.serviceTree(form));
            model.addAttribute("treeWell2", landMassService.treeWell(form.getLandMass_id(), null));
            model.addAttribute("tree1",form.getTreeSpecies());
            model.addAttribute("collect",landMassService.collect(form.getLandMass_id()));
            int index=1;
            for (Tree tree:treeManyDao.findAll(landMassService.whereGr(form, form.getLandMass_id()))) {
                form.setTreeSpecies(tree.getTreeSpecies());
                List<Tree> treeList = treeManyDao.findAll(landMassService.where(form, form.getLandMass_id()));
                if(Objects.nonNull(treeList)&&treeList.size()>0){
                    model.addAttribute("tree"+index++,form.getTreeSpecies() + treeList.size() + "棵");
                }
            }
            str = "admin/landMass/tree_list";
        }else {
            List<ApiSearch> apiSearches=landMassService.treeSpecies(form);
            model.addAttribute("apiSearches",apiSearches);
            model.addAttribute("tree", treeDao.findBytree_species());
            model.addAttribute("seektree", form);
        }
        return str;
    }

    @RequestMapping("/seek")
    public String seek(Model model) {
        List<ApiSearchModel> apiSearchModels = new ArrayList<>();
        model.addAttribute("apiSearchModels", apiSearchModels);
        model.addAttribute("tree", treeDao.findBytree_species());
        model.addAttribute("seektree", new SeekTreeForm());
        return "admin/landMass/landMass_list";
    }

    @RequestMapping("/treeWell")
    public String treeWell(Model model, Long landMass_id) {
        String strUrl = "admin/treeWell/treeWell_create";
        List<ApiTreeWell> apiTreeWells = landMassService.treeWell(landMass_id, null);
        if (Objects.nonNull(landMass_id)) {
            List<Tree> trees = treeDao.findTreeSpeciesGroup(landMass_id);
            int index = 1;
            for (Tree tree : trees) {
                model.addAttribute("tree" + index++, this.statistics(tree.getTreeSpecies(), landMass_id));
            }
        }
        LandMass landMass = landMassDao.findOne(landMass_id);
        model.addAttribute("collect",landMassService.collect(landMass_id));
        model.addAttribute("landMass", landMass);
        model.addAttribute("treeWell", apiTreeWells);
        if (Objects.nonNull(apiTreeWells) && apiTreeWells.size() > 0) {
            strUrl = "admin/treeWell/treeWell_list";
        }
        return strUrl;
    }

    @RequestMapping("/addLandMass")
    public String addLandMass(Long areaLocation_id, Model model) {
        if (Objects.nonNull(areaLocation_id)) {
            model.addAttribute("areaLocation", areaLocationDao.findOne(areaLocation_id));
        }
        return "admin/landMass/landMass_create";
    }

    @RequestMapping("/updateLandMass")
    public String updateLandMass(Long landMass_id, Model model) {
        if (Objects.nonNull(landMass_id)) {
            model.addAttribute("landMass", landMassDao.findOne(landMass_id));
        }
        return "admin/landMass/landMass_update";
    }

    @RequestMapping("/create")
    public String create(LandMassForm landMass) {
        if (Objects.nonNull(landMass.getAreaLocation_id())) {
            AreaLocation areaLocation = areaLocationDao.findOne(landMass.getAreaLocation_id());
            Long number = landMassDao.findMaxserialNumber(landMass.getAreaLocation_id());
            if (number == null) {
                number = 1l;
            } else {
                number++;
            }
            LandMass landMass1 = new LandMass();
            landMass1.setSerialNumber(number + "");
            landMass1.setBreadth(landMass.getBreadth());
            landMass1.setLength(landMass.getLength());
            landMass1.setAreaLocation(areaLocation);
            landMassDao.save(landMass1);
        }
        return "redirect:/admin/areaLocation/addDelete";
    }

    @RequestMapping("/update")
    public String update(LandMassForm landMass) {
        if (Objects.nonNull(landMass.getLandMass_id())) {
            LandMass landMass1 = landMassDao.findOne(landMass.getLandMass_id());
            landMass1.setLength(landMass.getLength());
            landMass1.setBreadth(landMass.getBreadth());
            landMassDao.save(landMass1);
        }
        return "redirect:/admin/areaLocation/addDelete";
    }

    @RequestMapping("/delete")
    public String delete(Long landMass_id) {
        if (Objects.nonNull(landMass_id)) {
            areaLocationService.deleteLand(landMass_id);
        }
        return "redirect:/admin/areaLocation/addDelete";
    }


    @RequestMapping("/rightdata")
    @ResponseBody
    public List<ApiTreeWell> rightdata(Long landMass_id) {
        List<ApiTreeWell> apiTreeWells = new ArrayList<>();
        if (Objects.nonNull(landMass_id)) {
            LandMass landMass = landMassDao.findOne(landMass_id);
            for (TreeWell value : landMass.getTreeWells()) {
                ApiTreeWell apiTreeWell = new ApiTreeWell();
                apiTreeWell.setUseState(value.getUseState());
                apiTreeWell.setSerialNumber(value.getSerialNumber());
                apiTreeWell.setSalesState(6);
                apiTreeWell.setTreeWell_id(value.getTreeWell_id());
                if (value.getUseState() == 1 && value.getTree_id() != 0) {
                    Tree tree = treeDao.findOne(value.getTree_id());
                    if (Objects.nonNull(tree)){
                        apiTreeWell.setTreeSpecies(tree.getTreeSpecies());
                        apiTreeWell.setHeight(tree.getHeight());
                        apiTreeWell.setChestBreadth(tree.getChestBreadth());
                        apiTreeWell.setCrownWidth(tree.getCrownWidth());
                        apiTreeWell.setClump(tree.getClump());
                        apiTreeWell.setHealthstate(tree.getHealthstate());
                        apiTreeWell.setLocation(tree.getLocation());
                        apiTreeWell.setSalesState(tree.getSalesState());
                        apiTreeWell.setResponseState(tree.getResponseState());
                        apiTreeWell.setRemark(tree.getRemark());
                        apiTreeWell.setPrice(tree.getPrice());
                        apiTreeWell.setSales(tree.getSales());
                        apiTreeWell.setTree_id(tree.getTree_id());
                    }
                }
                apiTreeWells.add(apiTreeWell);
            }
        }
        return apiTreeWells;
    }

    @RequestMapping("/remarks")
    @ResponseBody
    public void remarks(Long landMass_id,String remarks){
        LandMass landMass=landMassDao.findOne(landMass_id);
        if(Objects.nonNull(landMass)){
            landMass.setRemarks(remarks);
            landMassDao.save(landMass);
        }
    }

    @RequestMapping("/remarks/seek")
    public String remarksSeek(String remarks,Model model){
        List<LandMass> landMasses=new ArrayList<>();
      if(remarks!=null&&remarks.length()>0){
          for (LandMass land:landMassDao.findRemarks(remarks)) {
              LandMass landMass=new LandMass();
              landMass.setLandMass_id(land.getLandMass_id());
              landMass.setRemarks(land.getRemarks());
              landMass.setSerialNumber(land.getAreaLocation().getSerialNumber()+land.getSerialNumber());
              landMasses.add(landMass);
          }
      }
      model.addAttribute("landMasses",landMasses);
       return "admin/landMass/remarks";
    }

    @RequestMapping("/remarke/update")
    public String remarksSeek(Long landMass_id,String remarks){
       LandMass landMass=landMassDao.findOne(landMass_id);
       if(Objects.nonNull(landMass)){
           landMass.setRemarks(remarks);
           landMassDao.save(landMass);
       }
        return "redirect:/admin/landMass/remarks/seek?remarks="+remarks;
    }


}